package com.andf.sys.action;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.andf.basic.BaseAndfAction;
import com.andf.basic.IAndfAction;
import com.andf.common.DateUtil;
import com.andf.common.ToGson;
import com.andf.sys.form.TuserloginForm;
import com.andf.sys.model.TuserloginModel;
import com.andf.sys.service.ITuserloginService;

@SuppressWarnings("unchecked")
@Controller
@Scope("prototype")
public class TuserloginAction extends BaseAndfAction<TuserloginForm> {

	private static final long serialVersionUID = 1513783135003445927L;

	@Autowired
	private ITuserloginService tuserloginService;
	
	@Override
	public String dolist(){
		List<TuserloginForm> contactPersonList = tuserloginService
				.getModelAll(TuserloginForm.class, getEntity());
		
		if (contactPersonList != null && contactPersonList.size() > 0) {
			return ToGson.pageToJson(contactPersonList, 20,
					getEntity(), getResponse());
		}
		return IAndfAction.FORWARD_SUCCESS;
	}
	
	@Override
	public String doqryOne(){
		TuserloginForm contactPerson = (TuserloginForm) tuserloginService
				.getOneModel(TuserloginForm.class, getEntity());
		if (contactPerson != null) {
			return ToGson.objectToJson(contactPerson, getResponse());
		}
		return IAndfAction.FORWARD_SUCCESS;
	}
	
	@Override
	public String doinsert() throws Exception{
		if(StringUtils.isNotEmpty(getEntity().getLoginname())) {
			String curTime = DateUtil.getTodayTime();
			getEntity().setCreatedate(curTime);
			getEntity().setModifydate(curTime);
			getEntity().setPderrorcount("0");
			getEntity().setIsvalid("0");
			tuserloginService.saveModel(TuserloginForm.class, getEntity());
			return IAndfAction.FORWARD_SUCCESS;
		} else {
			return "page";
		}
	}
	
	@Override
	public String doupdate() throws RuntimeException, Exception {
		tuserloginService.updateModel(TuserloginForm.class, getEntity());
		return IAndfAction.FORWARD_SUCCESS;
	}
	
	@Override
	public String dodelete() throws RuntimeException, Exception {
		tuserloginService.deleteModelListUseTran(TuserloginForm.class, getIdsArrayResult());
		return IAndfAction.FORWARD_SUCCESS;
	}
}
